package com.sky.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.*;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import com.sky.service.DishService;
import com.sky.service.SetMealDishService;
import com.sky.service.SetMealService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealOverViewVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐模块")
public class SetmealController {


    @Autowired
    private SetMealService setMealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetMealDishService setMealDishService;


    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public Result pageQuery(SetmealPageQueryDTO setmealPageQueryDTO){
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(setmealPageQueryDTO.getStatus() != null, Setmeal::getStatus, setmealPageQueryDTO.getStatus());
        lambdaQueryWrapper.eq(setmealPageQueryDTO.getName() != null,Setmeal::getName,setmealPageQueryDTO.getName());
        lambdaQueryWrapper.like(setmealPageQueryDTO.getName() != null, Setmeal::getName, setmealPageQueryDTO.getName());


        Page<Setmeal> pageSetmeal = setMealService.page(new Page<>(setmealPageQueryDTO.getPage(),
                setmealPageQueryDTO.getPageSize()),lambdaQueryWrapper);

        Page<SetmealVO> pageVo = new Page<>();

        BeanUtils.copyProperties(pageSetmeal,pageSetmeal,"records");
        List<Setmeal> records = pageSetmeal.getRecords();

        List<SetmealVO> collect = records.stream().map((item) -> {
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(item, setmealVO);
            Category category = categoryService.getById(item.getCategoryId());
            if (category != null) {
                setmealVO.setCategoryName(category.getName());
            }
            return setmealVO;
        }).collect(Collectors.toList());
        pageVo.setRecords(collect);

        return Result.success(pageVo);
    }

    @PostMapping
    @ApiOperation(value = "分页查询")
    @Transactional
    public Result add(@RequestBody SetmealDTO setmealDTO){
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setMealService.save(setmeal);

        if(setmealDTO.getSetmealDishes() != null) {
            List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
            List<SetmealDish> dishes = setmealDishes.stream().map((item) -> {
                SetmealDish setmealDish = new SetmealDish();
                BeanUtils.copyProperties(item, setmealDish);
                setmealDish.setDishId(item.getDishId());
                setmealDish.setSetmealId(setmeal.getId());
                return setmealDish;
            }).collect(Collectors.toList());
            setMealDishService.saveBatch(dishes);
        }
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("分类启停")
    public Result startOrStop (@PathVariable Integer status,Long id){
        log.info("启用禁用账号：{},{}",status,id);
        Setmeal setmeal = setMealService.getById(id);
        setmeal.setStatus(status);
        setMealService.updateById(setmeal);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "页面回显")
    public Result<SetmealVO> selectId(@PathVariable Long id){
        log.info("回显页面信息");
        Setmeal setmeal = setMealService.getById(id);
        List<SetmealDish> setmealDishes = setMealDishService.lambdaQuery()
                .eq(SetmealDish::getSetmealId, id)
                .list();
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);

        return Result.success(setmealVO);
    }



}
